package com.inter.law.action;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.inter.law.dao.IlAcquisitionCostDAO;
import com.inter.law.dao.IlCaseDAO;
import com.inter.law.dao.IlCaseToClientDAO;
import com.inter.law.dao.IlCaseToContrayClientDAO;
import com.inter.law.dao.IlChargingDAO;
import com.inter.law.dao.IlClientDAO;
import com.inter.law.dao.IlContrayClientDAO;
import com.inter.law.dao.IlCostDAO;
import com.inter.law.dao.IlLawerDAO;
import com.inter.law.dao.IlTaskDAO;
import com.inter.law.entity.IlAcquisitionCost;
import com.inter.law.entity.IlCase;
import com.inter.law.entity.IlCaseToClient;
import com.inter.law.entity.IlCaseToContrayClient;
import com.inter.law.entity.IlCharging;
import com.inter.law.entity.IlClient;
import com.inter.law.entity.IlContrayClient;
import com.inter.law.entity.IlCost;
import com.inter.law.entity.IlLawer;
import com.inter.law.entity.IlTask;
import com.inter.law.utils.FileOperateUtils;
import com.inter.law.utils.PathUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 提交立案申请表
 * 
 * @author Administrator
 * 
 */
public class FilingApplication extends ActionSupport {
	private ApplicationContext ctx = new ClassPathXmlApplicationContext(
			"applicationContext.xml");
	private Map<String, Object> session = ActionContext.getContext()
			.getSession();
	// case中所有字段
	private Integer caseId;
	private Integer caseCharge;
	private Integer caseOffice;
	private String casePreType;
	private String caseType;
	private String caseAuxiliaryType;
	private Date caseDate;
	private String caseSource;
	private String casePartyType;
	private String caseParty;
	private String casePartyCompany;
	private String caseVictim;
	private String caseTrialGrade;
	private Date caseStartdate;
	private Date caseEnddate;
	private Double caseAcqcost;
	private String caseClient;
	private Boolean caseIsNonage;
	private Boolean caseIsTpsd;
	private String caseReason;
	private Integer caseClientType;
	private String caseContrayClient;
	private Integer caseContrayClientType;
	private String caseProfile;
	private Boolean caseIsSpecial;
	private String caseBilingMethod;
	private String caseBilingNotes;
	private String caseChargeReduction;
	private Integer caseBenefitType;
	private String caseNotes;
	private String caseProtocol;
	private Integer caseLawerId;
	private Integer caseStatus;
	private Integer caseNumber;
	private String caseName;
	// 需要与案件关联的实体中的字段List
	// 委托人中的字段List
	private List<String> clientName;
	private List<String> contrayClientName;
	// 对方当事人中的字段List
	private List<Integer> clientType;
	private List<Integer> contrayClientType;
	// 展业费中的字段List
	private List<Integer> alawerId;
	private List<Double> aratio;
	// 计费金额中的字段List
	private List<String> currencyType;
	private List<Double> agencyFee;
	private List<Double> travelExpense;
	private List<Date> protocolStartdate;
	private List<Date> protocolEnddate;
	private List<String> notes;
	// 费用中的字段List
	private List<Integer> clawerId;
	private List<Double> attorneyFee;
	private List<Double> ctravelExpense;
	private List<Double> hourExpense;
	// 任务中的字段List
	private List<Integer> tlawerId;
	private List<String> content;
	private List<Date> taskDate;

	// 需要与案件关联的实体List
	private List<IlClient> ilClients = new ArrayList<IlClient>();// 委托人List
	private List<IlContrayClient> ilContrayClients = new ArrayList<IlContrayClient>();// 对方当事人List
	private List<IlCaseToClient> ilCaseToClients = new ArrayList<IlCaseToClient>();// 案件关联委托人List
	private List<IlCaseToContrayClient> ilCaseToContrayClients = new ArrayList<IlCaseToContrayClient>();// 案件关联对方当事人List
	private List<IlAcquisitionCost> iacList = new ArrayList<IlAcquisitionCost>();// 展业费分配List
	private List<IlCharging> icList = new ArrayList<IlCharging>();// 计费金额分配List
	private List<IlCost> ilCostList = new ArrayList<IlCost>();// 费用（包括律师费和差旅费）分配List
	private List<IlTask> itList = new ArrayList<IlTask>();// 任务List

	private IlCase ilCase;// 案件

	// private List<E>
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		String username = (String) session.get("username");
		String normalPath = PathUtils.PATH + "/" + username + "/"+caseName;
		String tempPath = PathUtils.PATH + "/" + username + "/temp";
		String srcPath = tempPath + "/" + session.get("protocolFileName");
		String destPath = normalPath + "/" + session.get("protocolFileName");
		FileOperateUtils.cutGeneralFile(srcPath, normalPath);
		IlCaseDAO ilCaseDAO = IlCaseDAO.getFromApplicationContext(ctx);
		ilCase = getIlCase();
		ilCase.setCaseProtocol(destPath);
		IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ctx);
		System.out.println(username);
		List<IlLawer> list=ilLawerDAO.findByLawerUsername(username);
		if(list!=null&&list.size()>0){
			System.out.println("AAAAAAAAAAAAAAAAA");
			ilCase.setCaseLawerId(list.get(0).getLawerId());
			ilCaseDAO.save(ilCase);
			getObjectList();
			connect();
		}	else{
			System.out.println("BBBBBBBBBBBBAAAA");
		}
		return SUCCESS;
	}

	// 关联与案件相关的东西，比如：委托人，对方当事人,此时要操作的是案件关联委托人表与案件关联对方当事人表
	public void connect() {
		if (ilClients != null && ilClients.size() > 0) {
			IlCaseToClientDAO ilCaseToClientDAO = IlCaseToClientDAO
					.getFromApplicationContext(ctx);
			for (int i = 0; i < ilClients.size(); i++) {
				IlCaseToClient ilCaseToClient = new IlCaseToClient();
				ilCaseToClient.setClientId(ilClients.get(i).getId());
				ilCaseToClient.setCaseId(ilCase.getCaseId());
				ilCaseToClientDAO.save(ilCaseToClient);
			}
		}
		if (ilContrayClients != null && ilContrayClients.size() > 0) {
			IlCaseToContrayClientDAO ilCaseToContrayClientDAO = IlCaseToContrayClientDAO
					.getFromApplicationContext(ctx);
			for (int i = 0; i < ilContrayClients.size(); i++) {
				IlCaseToContrayClient ilCaseToContrayClient = new IlCaseToContrayClient();
				ilCaseToContrayClient.setContrayClientId(ilContrayClients
						.get(i).getId());
				ilCaseToContrayClient.setCaseId(ilCase.getCaseId());
				ilCaseToContrayClientDAO.save(ilCaseToContrayClient);
			}
		}

	}

	// 获取对象List，将从页面得到的List组装成对象List
	public void getObjectList() {
		if (clientName != null && clientName.size() > 0) {
			IlClientDAO ilClientDAO = IlClientDAO
					.getFromApplicationContext(ctx);
			for (int i = 0; i < clientName.size(); i++) {
				IlClient ilClient = new IlClient();
				ilClient.setName(clientName.get(i));
				ilClient.setCaseClientType(clientType.get(i));
				ilClientDAO.save(ilClient);
				ilClients.add(ilClient);
			}
		}
		if (contrayClientName != null && contrayClientName.size() > 0) {
			IlContrayClientDAO ilContrayClientDAO = IlContrayClientDAO
					.getFromApplicationContext(ctx);
			for (int i = 0; i < contrayClientName.size(); i++) {
				IlContrayClient ilContrayClient = new IlContrayClient();
				ilContrayClient.setName(contrayClientName.get(i));
				ilContrayClient.setCaseContrayClientType(contrayClientType
						.get(i));
				ilContrayClientDAO.save(ilContrayClient);
				ilContrayClients.add(ilContrayClient);
			}
		}
		if (alawerId != null && alawerId.size() > 0) {
			IlAcquisitionCostDAO ilAcquisitionCostDAO = IlAcquisitionCostDAO
					.getFromApplicationContext(ctx);
			for (int i = 0; i < alawerId.size(); i++) {
				IlAcquisitionCost ilAcquisitionCost = new IlAcquisitionCost();
				ilAcquisitionCost.setLawerId(alawerId.get(i));
				ilAcquisitionCost.setRatio(aratio.get(i));// 从页面传过来的是百分比（传的值不带百分号），这里说明保存的是百分点
				ilAcquisitionCost.setCaseId(ilCase.getCaseId());
				ilAcquisitionCostDAO.save(ilAcquisitionCost);
				iacList.add(ilAcquisitionCost);
			}
		}

		if (currencyType != null && currencyType.size() > 0) {
			IlChargingDAO ilChargingDAO = IlChargingDAO
					.getFromApplicationContext(ctx);
			System.out.println(currencyType.size() + "");
			for (int i = 0; i < currencyType.size(); i++) {
				IlCharging ilCharging = new IlCharging();
				ilCharging.setCurrencyType(currencyType.get(i));
				ilCharging.setAgencyFee(agencyFee.get(i));
				ilCharging.setTravelExpense(travelExpense.get(i));
				ilCharging.setProtocolStartdate(protocolStartdate.get(i));
				ilCharging.setProtocolEnddate(protocolEnddate.get(i));
				ilCharging.setNotes(notes.get(i));
				ilCharging.setCaseId(ilCase.getCaseId());
				ilChargingDAO.save(ilCharging);
				icList.add(ilCharging);
			}
		}
		if (clawerId != null && clawerId.size() > 0) {

			IlCostDAO ilCostDAO = IlCostDAO.getFromApplicationContext(ctx);
			for (int i = 0; i < clawerId.size(); i++) {
				IlCost ilCost = new IlCost();
				ilCost.setLawerId(clawerId.get(i));
				ilCost.setAttorneyFee(attorneyFee.get(i));
				ilCost.setTravelExpense(ctravelExpense.get(i));
				ilCost.setHourExpense(hourExpense.get(i));
				ilCost.setCaseId(ilCase.getCaseId());
				ilCostDAO.save(ilCost);
				ilCostList.add(ilCost);
			}
		}
		if (tlawerId != null && tlawerId.size() > 0) {
			IlTaskDAO ilTaskDAO = IlTaskDAO.getFromApplicationContext(ctx);
			for (int i = 0; i < tlawerId.size(); i++) {
				IlTask ilTask = new IlTask();
				ilTask.setLawerId(tlawerId.get(i));
			    ilTask.setStartDate(new Timestamp(taskDate.get(i).getTime()));
			    ilTask.setContent(content.get(i));
				ilTask.setCaseId(ilCase.getCaseId());
				ilTaskDAO.save(ilTask);
				itList.add(ilTask);
			}
		}

	}

	public IlCase getIlCase() {
		IlCase ilCase = new IlCase();
		ilCase.setCaseAcqcost(caseAcqcost);
		ilCase.setCaseAuxiliaryType(caseAuxiliaryType);
		ilCase.setCaseBenefitType(caseBenefitType);
		ilCase.setCaseBilingMethod(caseBilingMethod);
		ilCase.setCaseBilingNotes(caseBilingNotes);
		ilCase.setCaseChargeId(caseCharge);
		ilCase.setCaseChargeReduction(caseChargeReduction);
		ilCase.setCaseClient(caseClient);
		ilCase.setCaseClientType(caseClientType);
		ilCase.setCaseContrayClient(caseContrayClient);
		ilCase.setCaseContrayClientType(caseContrayClientType);
		ilCase.setCaseDate(caseDate);
		ilCase.setCaseEnddate(caseEnddate);
		ilCase.setCaseId(caseId);
		ilCase.setCaseIsNonage(caseIsNonage);
		ilCase.setCaseIsSpecial(caseIsSpecial);
		ilCase.setCaseIsTpsd(caseIsTpsd);
		ilCase.setCaseLawerId(caseLawerId);
		ilCase.setCaseNotes(caseNotes);
		ilCase.setCaseNumber(caseNumber);
		ilCase.setCaseOfficeId(caseOffice);
		ilCase.setCaseParty(caseParty);
		ilCase.setCasePartyCompany(casePartyCompany);
		ilCase.setCasePartyType(casePartyType);
		ilCase.setCasePreType(casePreType);
		ilCase.setCaseProfile(caseProfile);
		ilCase.setCaseProtocol(caseProtocol);
		ilCase.setCaseReason(caseReason);
		ilCase.setCaseSource(caseSource);
		ilCase.setCaseStartdate(caseStartdate);
		ilCase.setCaseStatus(caseStatus);
		ilCase.setCaseTrialGrade(caseTrialGrade);
		ilCase.setCaseType(caseType);
		ilCase.setCaseVictim(caseVictim);
		ilCase.setCaseName(caseName);
		return ilCase;
	}

	public Integer getCaseId() {
		return caseId;
	}

	public void setCaseId(Integer caseId) {
		this.caseId = caseId;
	}

	public Integer getCaseCharge() {
		return caseCharge;
	}

	public void setCaseCharge(Integer caseCharge) {
		this.caseCharge = caseCharge;
	}

	public Integer getCaseOffice() {
		return caseOffice;
	}

	public void setCaseOffice(Integer caseOffice) {
		this.caseOffice = caseOffice;
	}

	public String getCasePreType() {
		return casePreType;
	}

	public void setCasePreType(String casePreType) {
		this.casePreType = casePreType;
	}

	public String getCaseType() {
		return caseType;
	}

	public void setCaseType(String caseType) {
		this.caseType = caseType;
	}

	public String getCaseAuxiliaryType() {
		return caseAuxiliaryType;
	}

	public void setCaseAuxiliaryType(String caseAuxiliaryType) {
		this.caseAuxiliaryType = caseAuxiliaryType;
	}

	public Date getCaseDate() {
		return caseDate;
	}

	public void setCaseDate(Date caseDate) {
		this.caseDate = caseDate;
	}

	public String getCaseSource() {
		return caseSource;
	}

	public void setCaseSource(String caseSource) {
		this.caseSource = caseSource;
	}

	public String getCasePartyType() {
		return casePartyType;
	}

	public void setCasePartyType(String casePartyType) {
		this.casePartyType = casePartyType;
	}

	public String getCaseParty() {
		return caseParty;
	}

	public void setCaseParty(String caseParty) {
		this.caseParty = caseParty;
	}

	public String getCasePartyCompany() {
		return casePartyCompany;
	}

	public void setCasePartyCompany(String casePartyCompany) {
		this.casePartyCompany = casePartyCompany;
	}

	public String getCaseVictim() {
		return caseVictim;
	}

	public void setCaseVictim(String caseVictim) {
		this.caseVictim = caseVictim;
	}

	public String getCaseTrialGrade() {
		return caseTrialGrade;
	}

	public void setCaseTrialGrade(String caseTrialGrade) {
		this.caseTrialGrade = caseTrialGrade;
	}

	public Date getCaseStartdate() {
		return caseStartdate;
	}

	public void setCaseStartdate(Date caseStartdate) {
		this.caseStartdate = caseStartdate;
	}

	public Date getCaseEnddate() {
		return caseEnddate;
	}

	public void setCaseEnddate(Date caseEnddate) {
		this.caseEnddate = caseEnddate;
	}

	public Double getCaseAcqcost() {
		return caseAcqcost;
	}

	public void setCaseAcqcost(Double caseAcqcost) {
		this.caseAcqcost = caseAcqcost;
	}

	public String getCaseClient() {
		return caseClient;
	}

	public void setCaseClient(String caseClient) {
		this.caseClient = caseClient;
	}

	public Boolean getCaseIsNonage() {
		return caseIsNonage;
	}

	public void setCaseIsNonage(Boolean caseIsNonage) {
		this.caseIsNonage = caseIsNonage;
	}

	public Boolean getCaseIsTpsd() {
		return caseIsTpsd;
	}

	public void setCaseIsTpsd(Boolean caseIsTpsd) {
		this.caseIsTpsd = caseIsTpsd;
	}

	public String getCaseReason() {
		return caseReason;
	}

	public void setCaseReason(String caseReason) {
		this.caseReason = caseReason;
	}

	public Integer getCaseClientType() {
		return caseClientType;
	}

	public void setCaseClientType(Integer caseClientType) {
		this.caseClientType = caseClientType;
	}

	public String getCaseContrayClient() {
		return caseContrayClient;
	}

	public void setCaseContrayClient(String caseContrayClient) {
		this.caseContrayClient = caseContrayClient;
	}

	public Integer getCaseContrayClientType() {
		return caseContrayClientType;
	}

	public void setCaseContrayClientType(Integer caseContrayClientType) {
		this.caseContrayClientType = caseContrayClientType;
	}

	public String getCaseProfile() {
		return caseProfile;
	}

	public void setCaseProfile(String caseProfile) {
		this.caseProfile = caseProfile;
	}

	public Boolean getCaseIsSpecial() {
		return caseIsSpecial;
	}

	public void setCaseIsSpecial(Boolean caseIsSpecial) {
		this.caseIsSpecial = caseIsSpecial;
	}

	public String getCaseBilingMethod() {
		return caseBilingMethod;
	}

	public void setCaseBilingMethod(String caseBilingMethod) {
		this.caseBilingMethod = caseBilingMethod;
	}

	public String getCaseBilingNotes() {
		return caseBilingNotes;
	}

	public void setCaseBilingNotes(String caseBilingNotes) {
		this.caseBilingNotes = caseBilingNotes;
	}

	public String getCaseChargeReduction() {
		return caseChargeReduction;
	}

	public void setCaseChargeReduction(String caseChargeReduction) {
		this.caseChargeReduction = caseChargeReduction;
	}

	public Integer getCaseBenefitType() {
		return caseBenefitType;
	}

	public void setCaseBenefitType(Integer caseBenefitType) {
		this.caseBenefitType = caseBenefitType;
	}

	public String getCaseNotes() {
		return caseNotes;
	}

	public void setCaseNotes(String caseNotes) {
		this.caseNotes = caseNotes;
	}

	public String getCaseProtocol() {
		return caseProtocol;
	}

	public void setCaseProtocol(String caseProtocol) {
		this.caseProtocol = caseProtocol;
	}

	public Integer getCaseLawerId() {
		return caseLawerId;
	}

	public void setCaseLawerId(Integer caseLawerId) {
		this.caseLawerId = caseLawerId;
	}

	public Integer getCaseStatus() {
		return caseStatus;
	}

	public void setCaseStatus(Integer caseStatus) {
		this.caseStatus = caseStatus;
	}

	public Integer getCaseNumber() {
		return caseNumber;
	}

	public void setCaseNumber(Integer caseNumber) {
		this.caseNumber = caseNumber;
	}

	public List<IlAcquisitionCost> getIacList() {
		return iacList;
	}

	public void setIacList(List<IlAcquisitionCost> iacList) {
		this.iacList = iacList;
	}

	public List<IlCharging> getIcList() {
		return icList;
	}

	public void setIcList(List<IlCharging> icList) {
		this.icList = icList;
	}

	public List<IlCost> getIlCostList() {
		return ilCostList;
	}

	public void setIlCostList(List<IlCost> ilCostList) {
		this.ilCostList = ilCostList;
	}

	public List<String> getClientName() {
		return clientName;
	}

	public void setClientName(List<String> clientName) {
		this.clientName = clientName;
	}

	public List<String> getContrayClientName() {
		return contrayClientName;
	}

	public void setContrayClientName(List<String> contrayClientName) {
		this.contrayClientName = contrayClientName;
	}

	public List<Integer> getClientType() {
		return clientType;
	}

	public void setClientType(List<Integer> clientType) {
		this.clientType = clientType;
	}

	public List<Integer> getContrayClientType() {
		return contrayClientType;
	}

	public void setContrayClientType(List<Integer> contrayClientType) {
		this.contrayClientType = contrayClientType;
	}

	public List<Integer> getAlawerId() {
		return alawerId;
	}

	public void setAlawerId(List<Integer> alawerId) {
		this.alawerId = alawerId;
	}

	public List<Double> getAratio() {
		return aratio;
	}

	public void setAratio(List<Double> aratio) {
		this.aratio = aratio;
	}

	public List<String> getCurrencyType() {
		return currencyType;
	}

	public void setCurrencyType(List<String> currencyType) {
		this.currencyType = currencyType;
	}

	public List<Double> getAgencyFee() {
		return agencyFee;
	}

	public void setAgencyFee(List<Double> agencyFee) {
		this.agencyFee = agencyFee;
	}

	public List<Double> getTravelExpense() {
		return travelExpense;
	}

	public void setTravelExpense(List<Double> travelExpense) {
		this.travelExpense = travelExpense;
	}

	public List<Date> getProtocolStartdate() {
		return protocolStartdate;
	}

	public void setProtocolStartdate(List<Date> protocolStartdate) {
		this.protocolStartdate = protocolStartdate;
	}

	public List<Date> getProtocolEnddate() {
		return protocolEnddate;
	}

	public void setProtocolEnddate(List<Date> protocolEnddate) {
		this.protocolEnddate = protocolEnddate;
	}

	public List<String> getNotes() {
		return notes;
	}

	public void setNotes(List<String> notes) {
		this.notes = notes;
	}

	public List<Integer> getClawerId() {
		return clawerId;
	}

	public void setClawerId(List<Integer> clawerId) {
		this.clawerId = clawerId;
	}

	public List<Double> getAttorneyFee() {
		return attorneyFee;
	}

	public void setAttorneyFee(List<Double> attorneyFee) {
		this.attorneyFee = attorneyFee;
	}

	public List<Double> getCtravelExpense() {
		return ctravelExpense;
	}

	public void setCtravelExpense(List<Double> ctravelExpense) {
		this.ctravelExpense = ctravelExpense;
	}

	public List<Double> getHourExpense() {
		return hourExpense;
	}

	public void setHourExpense(List<Double> hourExpense) {
		this.hourExpense = hourExpense;
	}

	public List<IlClient> getIlClients() {
		return ilClients;
	}

	public void setIlClients(List<IlClient> ilClients) {
		this.ilClients = ilClients;
	}

	public List<IlContrayClient> getIlContrayClients() {
		return ilContrayClients;
	}

	public void setIlContrayClients(List<IlContrayClient> ilContrayClients) {
		this.ilContrayClients = ilContrayClients;
	}

	public List<IlCaseToClient> getIlCaseToClients() {
		return ilCaseToClients;
	}

	public void setIlCaseToClients(List<IlCaseToClient> ilCaseToClients) {
		this.ilCaseToClients = ilCaseToClients;
	}

	public List<IlCaseToContrayClient> getIlCaseToContrayClients() {
		return ilCaseToContrayClients;
	}

	public void setIlCaseToContrayClients(
			List<IlCaseToContrayClient> ilCaseToContrayClients) {
		this.ilCaseToContrayClients = ilCaseToContrayClients;
	}

	public void setIlCase(IlCase ilCase) {
		this.ilCase = ilCase;
	}

	public List<Integer> getTlawerId() {
		return tlawerId;
	}

	public void setTlawerId(List<Integer> tlawerId) {
		this.tlawerId = tlawerId;
	}

	public List<String> getContent() {
		return content;
	}

	public void setContent(List<String> content) {
		this.content = content;
	}

	public List<Date> getTaskDate() {
		return taskDate;
	}

	public void setTaskDate(List<Date> taskDate) {
		this.taskDate = taskDate;
	}

	public String getCaseName() {
		return caseName;
	}

	public void setCaseName(String caseName) {
		this.caseName = caseName;
	}
	
	

}
